<?php

namespace addons\cash\model;

use think\Model;


class Cash extends Model
{

    

    

    // 表名
    protected $name = 'cash';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'way_text',
        'status_text',
        'check_time_text',
        'create_time_text'
    ];

    private $config = [];
    

    
    public function getWayList()
    {
        return ['1' => __('Way 1'), '2' => __('Way 2')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getWayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['way']) ? $data['way'] : '');
        $list = $this->getWayList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCheckTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['check_time']) ? $data['check_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCheckTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    /**
     * 检查余额
     * @param $money
     * @param $user_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkMoney($money,$user_id){
        $userInfo = db('user')->where('id',$user_id)->find();
        if(empty($userInfo)){
            $this->error = "无此用户";return false;
        }
        if($money>$userInfo['money']){
            $this->error = "余额不足";return false;
        }
    }

    /**
     * 检测最小提现金额
     * @param $money
     */
    public function checkMinCashMoney($money){
        $config = get_addon_config('cash');
        if(isset($config['min']) && $config['min']>0){
            if($money<$config['min']){
                $this->error = "最小提现金额为：".$config['min'];
            }
        }
    }

    public function _add($money,$way,$user_id){
        $this->checkMinCashMoney($money);
        $this->checkMoney($money,$user_id);
    }
}
